// pages/groupdata/groupdata.js
import config from '~/configs/configs'
import baseuser from '~/utils/users.js'
import baseteam from '~/utils/teamleader.js'
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        "orderbyid": '',

        express: false,
        expressselect: '',
        expresscompanys: ["我不想要了",
            "规格选错了",
            "颜色不符合",
        ],

        "headRemarks": "",

        "expressName": "快递公司",
        stopImg: [],
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 50);
    },
    onClose() {
        this.setData({
            express: false
        })
    },
    onChange(event) {
        this.setData({
            expressselect: event.detail,
        });
    },

    onClick(event) {
        const { name } = event.currentTarget.dataset;
        this.setData({
            expressselect: name,
        });
    },
    slideafterRead(event) {
        const { file } = event.detail;
        const stopImg = this.data.stopImg;
        stopImg.push(...file);
        this.setData({ stopImg });
    },
    slidedel(event) {
        let stopImg = this.data.stopImg;
        stopImg.splice(event.detail.index, 1)
        this.setData({ stopImg });
    },
    openexpress() {
        this.setData({
            express: true
        })
    },
    async ourdersdata() {
        let reqdata = {
            id: this.data.orderbyid
        }
        let resdata = await baseteam.orderbyid(reqdata)
        console.log(resdata)
        if (resdata.message == "操作成功") {
            this.setData({
                ourdersdetail: resdata.data.result
            })
            if (resdata.data.result.status == 1) {
                this.setData({
                    active: 1
                })
                return false
            }
            if (resdata.data.result.status == 2) {
                this.setData({
                    active: 2
                })
                return false
            }
            // ????
            // if (resdata.data.result.status == 5) {
            //     this.setData({
            //         active:4
            //     })
            //     return false
            // }
        }
    },

    filesUpload(paths) {
        return new Promise((resolve, reject) => {
            // wx.showLoading({
            //     title: '上传中, 请稍等..',
            //     mask: true
            // })
            let uploadCount = 0; // 上传计数
            const resources = [] // 上传结果
            //上传图片
            for (const path of (Array.isArray(paths) ? paths : [paths])) {
                wx.uploadFile({
                    url: config.baseUrl + baseuser.adminossfileupload,
                    filePath: path.url,
                    name: 'file',
                    timeout: 60 * 1000,
                    success: function (res) {
                        let data = JSON.parse(res.data);
                        if (data.code == 200) {
                            resources.push(data.data.map.url)
                        }
                    },
                    complete: () => {
                        uploadCount++
                        if (resources.length == (Array.isArray(paths) ? paths.length : 1)) {
                            // wx.hideLoading()
                            if (resources.length == 0) {
                                // showToast('上传失败')
                                reject('上传失败')
                            } else {
                                // showToast(paths instanceof String ? '上传成功' : '成功上传：' + resources.length + '张')
                                resolve(resources)
                            }
                        }
                    }
                });
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            orderbyid: options.orderbyid
        })
        this.ourdersdata()
    },
    afterRead(e) {
        console.log(e)
    },
    nextqrcode() {
        let that = this
        // 允许从相机和相册扫码
        wx.scanCode({
            // scanType:
            success(res) {
                console.log(res)
                that.setData({
                    value: res.result
                })
            }
        })
    },

    onConfirmexpress(items) {
        this.setData({
            expressselect: items.detail.index
        })
        this.onClose()
    },
    async bindsubmit() {
        let stopImglist = []
        if (this.data.stopImg.length != 0) {
            stopImglist = await this.filesUpload(this.data.stopImg)
        }else{
            wx.showToast({
                title: '二维码未上传',
                icon: 'none',
                duration: 2000
            })
            return false
        }

        let reqdata = {
            "id": this.data.ourdersdetail.id,
            'ktQrCode':JSON.stringify(stopImglist)
        }
        let resdata = await baseteam.OrderkaiTuan(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '申请成功，即将返回上个页面',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 1200);
        } else {
            wx.showToast({
                title: '申请失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.ourdersdata()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})